package demo12;

import java.util.Scanner;

class Person implements Cloneable{
    @Override
    protected  Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
}

public class Test {
    public static void main(String[] args) {

    }
    public static void test1(){
        int[] array={1,2,4};
        System.out.println(array[10]);
    }

    public static void main12(String[] args) {
        try{
            test1();
            System.out.println("结束try的代码");
        }catch (ArrayIndexOutOfBoundsException e){

        }
        System.out.println("fsdafasfsaf");
    }
    public static int testFinally(){
        try{
            return 10;
        }catch (IncompatibleClassChangeError e){
            System.out.println("处理IncompatibleClassChangeError ");
            e.printStackTrace();
        }finally {
            System.out.println("finally中代码");
            return 20;
        }
    }

    public static void main11(String[] args) {
        System.out.println(testFinally());
    }
    public static int getData(){
        try(Scanner scanner =new Scanner(System.in)){
            int data =scanner.nextInt();
            return data;
        }catch (IncompatibleClassChangeError e){
            e.printStackTrace();
        }finally {
            System.out.println("finally中代码");
        }
        System.out.println("try-catch-finally代码");
        return 0;
    }
    public static void main10(String[] args) {
        //finally一般用于资源的释放
        int data =getData();
        System.out.println(data);
    }
    public static void main9(String[] args) {
        int[] array ={1,2,3};
        try{
//            System.out.println(array[21]);
//            array =null;
            System.out.println(array.length);
        }catch (NullPointerException e){
            System.out.println("处理NullPointerException 异常");
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("处理Exception！！！");
            e.printStackTrace();
        }finally{
            System.out.println("finally被执行了！！！【不管是否发生异常finally都会被执行]");
        }
    }
    public static void main8(String[] args) {
        int[] array ={1,2,3};
        try{
//            System.out.println(array[21]);
//            array =null;
            System.out.println(array.length);
        }catch (NullPointerException e){
            System.out.println("处理NullPointerException 异常");
            e.printStackTrace();
        }catch (Exception e){
            System.out.println("处理Exception");
            e.printStackTrace();

    }
        System.out.println("after");
    }
    public static void main7(String[] args) {
        int[] array ={1,2,3};
        try{
            System.out.println(array[21]);
            array =null;
            System.out.println(array.length);
        }catch (ArrayIndexOutOfBoundsException|NullPointerException e){
            System.out.println("处理ArrayIndexOutOfBoundsException|NullPointerException 异常");
            e.printStackTrace();
        }
        System.out.println("after");
    }
    public static void function(int a) throws CloneNotSupportedException{
        if(a==0){
            throw new CloneNotSupportedException();
        }
    }
    public static void main6(String[] args) {
        try{
            function(10);
            System.out.println("尝试寻找！！！");
        }catch(CloneNotSupportedException e){
            System.out.println("捕获到了异常，处理异常");
        }
        System.out.println("程序正常运行");
    }
    public static void testException(int a) {
        if (a == 0){
            throw new ArithmeticException("a==0");
            //System.out.println();err
        }
    }
    public static void main5(String[] args) {
        testException(0);
    }
    public static void main4(String[] args) throws CloneNotSupportedException,NullPointerException{
        Person person =new Person();
        Person person1 =(Person) person.clone();
    }
    public static void main3(String[] args) {
        //System.out.println(10/0);'java.lang.ArithmeticException'  算术异常
        //int[] arr ={1,2,3};
        //System.out.println(arr[10]);java.lang.ArrayIndexOutOfBoundsException 数组越界异常
        /*int [] arr =null;
        System.out.println(arr.length);NullPointerException 空指针异常*/
        System.out.println("abcdegfg");
    }
    public static int countSegments(String s){
        int len =s.length();
        if(len==0){
            return 0;
        }
        int count =0;
        String[] ss=s.split(" ");
        for(String r:ss){
            if(r.length()!=0){
                count++;
            }
        }
        return count;
    }
    public static void main2(String[] args) {
        int count =countSegments("!!!!!   a,fasfa");
        System.out.println(count);
    }
    public String toLowerCase(String s){
        StringBuilder stringBuilder =new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch =s.charAt(i);
            //判断当前字符，是不是字母
            if(Character.isLetter(ch)){
                //判断当前是不是大写字母
                if(Character.isUpperCase(ch)){
                    ch =(char)(ch+32);
                }
            }
            stringBuilder.append(ch);
        }
        return stringBuilder.toString();
    }
    String str =new String("good");
    char [] ch ={'a','p','p','e'};

    public static void main1(String[] args) {
        Test ex = new Test();
        ex.change(ex.str,ex.ch);
        System.out.println(ex.str+" and" );
        System.out.println(ex.ch);
    }

    private void change(String str, char[] ch) {
        str ="test ok";
        ch[0] ='g';
    }
    public static void func1(int[] array){
        array =new int[10];
    }
    public static void func2(int[] array){
        array[0] =10;
    }
}
